package com.zygh.policeasset.controller;


import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.hutool.core.date.DateUtil;
import com.zygh.policeasset.component.AssetsCode;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.ApprovalProcess;
import com.zygh.policeasset.pojo.Basics;
import com.zygh.policeasset.pojo.Hardwit;
import com.zygh.policeasset.service.ApprovalProcessService;
import com.zygh.policeasset.service.AssetOverhaulService;
import com.zygh.policeasset.service.AssetTransferService;
import com.zygh.policeasset.service.AssetsApprovalStepsService;
import com.zygh.policeasset.service.AssetsIdTypeService;
import com.zygh.policeasset.service.AssetsStepsService;
import com.zygh.policeasset.service.BasicsService;
import com.zygh.policeasset.service.CompletedSerivce;
import com.zygh.policeasset.service.DailyAssetsService;
import com.zygh.policeasset.service.DailyProjectService;
import com.zygh.policeasset.service.FinalStatementService;
import com.zygh.policeasset.service.HardwitService;
import com.zygh.policeasset.service.SettlementService;
import com.zygh.policeasset.service.SoftwitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"资产审批"})
@RequestMapping({"/assetsapproval"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/AssetsApprovalController.class */
public class AssetsApprovalController {
    @Autowired
    ApprovalProcessService approvalProcessService;
    @Autowired
    AssetsApprovalStepsService assetsApprovalStepsService;
    @Autowired
    BasicsService basicsService;
    @Autowired
    SoftwitService softwitService;
    @Autowired
    AssetsCode assetsCode;
    @Autowired
    AssetsIdTypeService assetsIdTypeService;
    @Autowired
    HardwitService hardwitService;
    @Autowired
    AssetOverhaulService assetOverhaulService;
    @Autowired
    AssetTransferService assetTransferService;
    @Autowired
    FinalStatementService finalStatementService;
    @Autowired
    DailyAssetsService dailyAssetsService;
    @Autowired
    DailyProjectService dailyProjectService;
    @Autowired
    AssetsStepsService assetsStepsService;
    @Autowired
    CompletedSerivce completedSerivce;
    @Autowired
    SettlementService settlementService;

    @RequestMapping(value = {"/basics"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("基础资产大修、转移、拆除审批")
    public Object basics(@RequestBody JSONObject json) {
        try {
            List<Basics> basicsList = (List) JSONArray.toCollection(JSONArray.fromObject(json.get("basicsList").toString()), Basics.class);
            String assetsType = json.get("assetsType").toString();
            if (this.assetsStepsService.queryByStepsType(assetsType) > 0) {
                String user_name = json.get("user_name").toString();
                String approval_opinion = json.get("approval_opinion").toString();
                for (Basics basics : basicsList) {
                    this.basicsService.updateStatus(assetsType, basics.getId());
                    add(user_name, approval_opinion, basics.getId(), basics.getNextId(), assetsType);
                    if ("0".equals(basics.getAddOrUpdate())) {
                        this.assetsIdTypeService.add(basics.getId(), basics.getNextId(), assetsType, DateUtil.now());
                        if (assetsType.equals("jc2")) {
                            this.assetOverhaulService.add(basics.getId(), assetsType, basics.getOverhaulStartMoney(), basics.getOverhaulRemark(), 1, DateUtil.now(), basics.getOverhaulDepreciationPeriod(), basics.getOverhaulTime(), "", "");
                        } else if (assetsType.equals("jc3")) {
                            this.assetTransferService.add(basics.getId(), assetsType, basics.getAddress(), basics.getTransferRemark(), 1, DateUtil.now());
                        }
                    } else if ("1".equals(basics.getAddOrUpdate())) {
                        this.assetsIdTypeService.updateStepsId(basics.getId(), basics.getNextId(), assetsType, DateUtil.now());
                    } else {
                        this.assetsIdTypeService.updateStepsId(basics.getId(), basics.getNextId(), assetsType, DateUtil.now());
                        if (assetsType.equals("jc2")) {
                            this.assetOverhaulService.add(basics.getId(), assetsType, basics.getOverhaulStartMoney(), basics.getOverhaulRemark(), 1, DateUtil.now(), basics.getOverhaulDepreciationPeriod(), basics.getOverhaulTime(), "", "");
                        } else if (assetsType.equals("jc3")) {
                            this.assetTransferService.add(basics.getId(), assetsType, basics.getAddress(), basics.getTransferRemark(), 1, DateUtil.now());
                        }
                    }
                    if (-1 == basics.getNextId().intValue()) {
                        if (assetsType.equals("jc2")) {
                            this.basicsService.updatePlaceByStartMoney(basics.getOverhaulStartMoney(), basics.getOverhaulDepreciationPeriod(), basics.getId(), "启用", basics.getOverhaulTime());
                            this.assetOverhaulService.add(basics.getId(), assetsType, basics.getStartMoney(), "", 0, DateUtil.now(), basics.getDepreciationPeriod(), basics.getOverhaulTime(), basics.getCreateTime(), basics.getStopRunTime());
                            this.assetOverhaulService.delete(basics.getId(), assetsType, 1);
                        } else if (assetsType.equals("jc3")) {
                            this.basicsService.updatePlaceById(basics.getAddress(), basics.getId());
                            this.assetTransferService.add(basics.getId(), assetsType, basics.getPlace(), "", 0, DateUtil.now());
                            this.assetTransferService.delete(basics.getId(), assetsType, 1);
                        } else if (assetsType.equals("jc4")) {
                            this.basicsService.updateStatus("jc4", basics.getId());
                        }
                    }
                    if (1 == basics.getNextId().intValue()) {
                        if (assetsType.equals("jc2")) {
                            this.assetOverhaulService.delete(basics.getId(), assetsType, 1);
                        } else if (assetsType.equals("jc3")) {
                            this.assetTransferService.delete(basics.getId(), assetsType, 1);
                        }
                    }
                }
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return new CommonResult().validateFailed("请添加审批流程");
    }

    @RequestMapping(value = {"/hardwit"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("智能硬件资产大修、转移、拆除审批")
    public Object hardwit(@RequestBody JSONObject json) {
        try {
            List<Hardwit> basicsList = (List) JSONArray.toCollection(JSONArray.fromObject(json.get("basicsList").toString()), Hardwit.class);
            String assetsType = json.get("assetsType").toString();
            if (this.assetsStepsService.queryByStepsType(assetsType) > 0) {
                String user_name = json.get("user_name").toString();
                String approval_opinion = json.get("approval_opinion").toString();
                for (Hardwit hardwit : basicsList) {
                    this.hardwitService.updateStatus(assetsType, hardwit.getId());
                    add(user_name, approval_opinion, hardwit.getId(), hardwit.getNextId(), assetsType);
                    if ("0".equals(hardwit.getAddOrUpdate())) {
                        this.assetsIdTypeService.add(hardwit.getId(), hardwit.getNextId(), assetsType, DateUtil.now());
                        if (assetsType.equals("znh2")) {
                            this.assetOverhaulService.add(hardwit.getId(), assetsType, hardwit.getOverhaulStartMoney(), hardwit.getOverhaulRemark(), 1, DateUtil.now(), hardwit.getOverhaulDepreciationPeriod(), hardwit.getOverhaulTime(), "", "");
                        } else if (assetsType.equals("znh3")) {
                            this.assetTransferService.add(hardwit.getId(), assetsType, hardwit.getAddress(), hardwit.getTransferRemark(), 1, DateUtil.now());
                        }
                    } else if ("1".equals(hardwit.getAddOrUpdate())) {
                        this.assetsIdTypeService.updateStepsId(hardwit.getId(), hardwit.getNextId(), assetsType, DateUtil.now());
                    } else {
                        this.assetsIdTypeService.updateStepsId(hardwit.getId(), hardwit.getNextId(), assetsType, DateUtil.now());
                        if (assetsType.equals("znh2")) {
                            this.assetOverhaulService.add(hardwit.getId(), assetsType, hardwit.getOverhaulStartMoney(), hardwit.getOverhaulRemark(), 1, DateUtil.now(), hardwit.getOverhaulDepreciationPeriod(), hardwit.getOverhaulTime(), "", "");
                        } else if (assetsType.equals("znh3")) {
                            this.assetTransferService.add(hardwit.getId(), assetsType, hardwit.getAddress(), hardwit.getTransferRemark(), 1, DateUtil.now());
                        }
                    }
                    if (-1 == hardwit.getNextId().intValue()) {
                        if (assetsType.equals("znh2")) {
                            this.hardwitService.updatePlaceByStartMoney(hardwit.getOverhaulStartMoney(), hardwit.getOverhaulDepreciationPeriod(), hardwit.getId(), "启用", hardwit.getOverhaulTime());
                            this.assetOverhaulService.add(hardwit.getId(), assetsType, hardwit.getStartMoney(), "", 0, DateUtil.now(), hardwit.getDepreciationPeriod(), hardwit.getOverhaulTime(), hardwit.getCreateTime(), hardwit.getStopRunTime());
                            this.assetOverhaulService.delete(hardwit.getId(), assetsType, 1);
                        } else if (assetsType.equals("znh3")) {
                            this.hardwitService.updatePlaceById(hardwit.getAddress(), hardwit.getId());
                            this.assetTransferService.add(hardwit.getId(), assetsType, hardwit.getPlace(), "", 0, DateUtil.now());
                            this.assetTransferService.delete(hardwit.getId(), assetsType, 1);
                        } else if (assetsType.equals("jc4")) {
                            this.basicsService.updateStatus("jc4", hardwit.getId());
                        }
                    }
                    if (1 == hardwit.getNextId().intValue()) {
                        if (assetsType.equals("znh2")) {
                            this.assetOverhaulService.delete(hardwit.getId(), assetsType, 1);
                        } else if (assetsType.equals("znh3")) {
                            this.assetTransferService.delete(hardwit.getId(), assetsType, 1);
                        }
                    }
                }
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return new CommonResult().validateFailed("请添加审批流程");
    }

    @RequestMapping(value = {"/dailyAssetSubmit"}, method = {RequestMethod.GET})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("日常资产新建提交、审批")
    public Object dailyAssetSubmit(@RequestParam(value = "approval_opinion", required = false) String approval_opinion, @RequestParam("user_name") String user_name, @RequestParam("status") String status, @RequestParam("build_num") String build_num) {
        try {
            ApprovalProcess approvalProcess = new ApprovalProcess();
            if ("2".equals(status)) {
                this.dailyProjectService.updateByid(Integer.valueOf(Integer.parseInt(status)), Integer.valueOf(Integer.parseInt(build_num)));
                approvalProcess.setApprovalId(-1);
            }
            if ("3".equals(status)) {
                this.dailyProjectService.updateByid(3, Integer.valueOf(Integer.parseInt(build_num)));
                approvalProcess.setApprovalId(1);
            } else if ("1".equals(status)) {
                this.dailyProjectService.updateByid(1, Integer.valueOf(Integer.parseInt(build_num)));
                approvalProcess.setApprovalId(2);
            }
            approvalProcess.setAssetsId(Integer.valueOf(Integer.parseInt(build_num)));
            approvalProcess.setApprovalOpinion(approval_opinion);
            approvalProcess.setOperatingTime(DateUtil.now());
            approvalProcess.setUserName(user_name);
            approvalProcess.setAssetsType("da");
            this.approvalProcessService.insert(approvalProcess);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return new CommonResult().success();
    }

    @RequestMapping(value = {"/basicssubmit"}, method = {RequestMethod.GET})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("基础资产新建提交、审批")
    public Object basicsSubmit(@RequestParam("assetsType") String assetsType, @RequestParam("approval_opinion") String approval_opinion, @RequestParam("user_name") String user_name, @RequestParam("status") String status, @RequestParam("build_num") String build_num, @RequestParam("nextId") Integer nextId, @RequestParam("addOrUpdate") Integer addOrUpdate, @RequestParam("sectionId") Integer sectionId, @RequestParam("sectionName") String sectionName) {
        try {
            if (this.assetsStepsService.queryByStepsType("jc1") > 0) {
                if (-1 == nextId.intValue()) {
                    this.settlementService.updateStatusAssets(status, build_num, sectionId);
                }
                if (1 == nextId.intValue()) {
                    this.settlementService.updateStatusAssets("0", build_num, sectionId);
                }
                if (2 == nextId.intValue()) {
                    this.settlementService.updateStatusAssets(status, build_num, sectionId);
                }
                this.basicsService.updatestatusByBuildNum(assetsType, build_num);
                this.approvalProcessService.insertToSelect(nextId, assetsType, approval_opinion, user_name, DateUtil.now(), build_num, sectionName);
                if (addOrUpdate.equals(0)) {
                    this.assetsIdTypeService.selectToInsert(nextId, assetsType, DateUtil.now(), build_num, sectionName);
                    this.finalStatementService.updateStatusAssets(status, build_num, sectionId);
                } else {
                    this.assetsIdTypeService.selectToUpdate(nextId, assetsType, DateUtil.now(), build_num, sectionName);
                }
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return new CommonResult().validateFailed("请添加审批流程");
    }

    @RequestMapping(value = {"/intelligentbmit"}, method = {RequestMethod.GET})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("智能新建提交、审批")
    public Object intelligentbmit(@RequestParam("approval_opinion") String approval_opinion, @RequestParam("user_name") String user_name, @RequestParam("status") String status, @RequestParam("build_num") String build_num, @RequestParam("nextId") Integer nextId, @RequestParam("addOrUpdate") Integer addOrUpdate, @RequestParam("assetsType") String assetsType, @RequestParam("sectionId") Integer sectionId, @RequestParam("sectionName") String sectionName) {
        try {
            if (this.assetsStepsService.queryByStepsType("jc1") > 0) {
                if (-1 == nextId.intValue()) {
                    this.settlementService.updateStatusAssets(status, build_num, sectionId);
                }
                if (1 == nextId.intValue()) {
                    this.settlementService.updateStatusAssets("0", build_num, sectionId);
                }
                if (2 == nextId.intValue()) {
                    this.settlementService.updateStatusAssets(status, build_num, sectionId);
                }
                this.hardwitService.updatestatusByBuildNum("znh1", build_num);
                this.softwitService.updatestatusByBuildNum("zns1", build_num);
                this.approvalProcessService.insertToSelecth(nextId, "znh1", approval_opinion, user_name, DateUtil.now(), build_num, sectionName);
                this.approvalProcessService.insertToSelects(nextId, "zns1", approval_opinion, user_name, DateUtil.now(), build_num, sectionName);
                if (0 == addOrUpdate.intValue()) {
                    this.assetsIdTypeService.selectToInserts(nextId, "zns1", DateUtil.now(), build_num, sectionName);
                    this.assetsIdTypeService.selectToInserth(nextId, "znh1", DateUtil.now(), build_num, sectionName);
                    this.finalStatementService.updateStatusAssets(status, build_num, sectionId);
                } else {
                    this.assetsIdTypeService.selectToUpdates(nextId, "zns1", DateUtil.now(), build_num, sectionName);
                    this.assetsIdTypeService.selectToUpdateh(nextId, "znh1", DateUtil.now(), build_num, sectionName);
                }
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return new CommonResult().validateFailed("请添加审批流程");
    }

    public int add(String user_name, String approval_opinion, Integer id, Integer nextId, String AssetsType) {
        ApprovalProcess approvalProcess = new ApprovalProcess();
        approvalProcess.setUserName(user_name);
        approvalProcess.setApprovalOpinion(approval_opinion);
        approvalProcess.setAssetsId(id);
        approvalProcess.setApprovalId(nextId);
        approvalProcess.setAssetsType(AssetsType);
        approvalProcess.setOperatingTime(DateUtil.now());
        return this.approvalProcessService.insert(approvalProcess);
    }

    @RequestMapping(value = {"/findapprovalcount"}, method = {RequestMethod.POST})
    @ApiOperation("查询需审批条数")
    public Object findapprovalcount(@RequestBody JSONObject json) {
        System.out.println(json);
        JSONArray jsonArray = JSONArray.fromObject(json.get(NormalExcelConstants.DATA_LIST).toString());
        JSONArray jsonArray1 = new JSONArray();
        String roleId = json.get("roleId").toString();
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < jsonArray.size(); i++) {
            String a = jsonArray.getJSONObject(i).get("assetsStatus").toString();
            String b = jsonArray.getJSONObject(i).get("buildNum").toString();
            if ("jc2".equals(a) || "jc3".equals(a) || "jc4".equals(a)) {
                Basics basics = new Basics();
                basics.setBuildNum(b);
                basics.setAssetsStatus(a);
                basics.setRoleId(roleId);
                int basiccount = this.basicsService.findByRoleIdToCount(basics);
                jsonObject.put("buildNum", b);
                jsonObject.put("count", Integer.valueOf(basiccount));
                jsonArray1.add(jsonObject);
                jsonObject.clear();
            } else if ("znh2".equals(a) || "znh3".equals(a) || "znh4".equals(a)) {
                Hardwit hardwit = new Hardwit();
                hardwit.setBuildNum(b);
                hardwit.setAssetsStatus(a);
                hardwit.setRoleId(roleId);
                int hardwitcount = this.hardwitService.findByRoleIdToCount(hardwit);
                jsonObject.put("buildNum", b);
                jsonObject.put("count", Integer.valueOf(hardwitcount));
                jsonArray1.add(jsonObject);
                jsonObject.clear();
            }
        }
        return new CommonResult().success(jsonArray1);
    }

    @RequestMapping(value = {"/findidbytype"}, method = {RequestMethod.GET})
    @ApiOperation("查询资产以前是否有转移或大修，如有修改assets_id_type")
    public Object findidbytype(@RequestParam("assetsid") Integer assetsid, @RequestParam("type") String type) {
        try {
            int a = 0;
            if (this.assetsIdTypeService.findidbytype(assetsid, type) > 0) {
                a = 2;
            }
            return new CommonResult().success(Integer.valueOf(a));
        } catch (Exception e) {
            return new CommonResult().validateFailed(e.toString());
        }
    }
}